package com.cmccgz.udacs_landlord_webapp.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.bean.RenterBean;

import java.util.List;

/**
 * @author Denis
 * @ClassName: PeopleFuncAdapter
 * @Description: 租客列表适配器
 * @date 2017/6/7 15:24
 */

public class PeopleAdapter extends DelegateAdapter.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private LayoutHelper helper;
    private List<RenterBean> datas;
    private OnPeopleClickListener onPeopleClickListener;

    public PeopleAdapter(Context context, LayoutHelper helper, List<RenterBean> datas) {
        this.context = context;
        this.helper = helper;
        this.datas = datas;
    }

    public void setOnPeopleClickListener(OnPeopleClickListener onPeopleClickListener) {
        this.onPeopleClickListener = onPeopleClickListener;
    }

    public void setDatas(List<RenterBean> datas) {
        this.datas = datas;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return helper;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case -1:
                return new EmptyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_empty, parent, false));
            case 1:
                return new PeopleHolder(LayoutInflater.from(context).inflate(R.layout.item_people, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case -1:
                ((EmptyViewHolder) holder).tv_empty.setText("暂无租客，下拉可刷新！");
                break;
            case 1:
                bindPeopleDatas((PeopleHolder) holder, position);
                break;
        }
    }

    public void bindPeopleDatas(PeopleHolder holder, int position) {
        final RenterBean renterBean = datas.get(position);
        PeopleHolder peopleHolder = holder;
        peopleHolder.tvName.setText(renterBean.user_name);
        peopleHolder.tvAddr.setText(renterBean.house_name);
        peopleHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onPeopleClickListener != null) {
                    onPeopleClickListener.onPeopleClickListener(renterBean);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas == null||datas.size()==0 ? 1 : datas.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (datas == null || datas.size() == 0) {
            return -1;
        } else {
            return 1;
        }
    }

    protected class PeopleHolder extends RecyclerView.ViewHolder {
        public TextView tvName;
        public TextView tvAddr;

        public PeopleHolder(View itemView) {
            super(itemView);
            tvName = (TextView) itemView.findViewById(R.id.tv_name);
            tvAddr = (TextView) itemView.findViewById(R.id.tv_address);
        }
    }

    public interface OnPeopleClickListener {
        public void onPeopleClickListener(RenterBean renterBean);
    }

    public void addData(List<RenterBean> addData) {
        if (this.datas != null && datas.size() > 0) {
            this.datas.addAll(addData);
            notifyDataSetChanged();
        }
    }

    private class EmptyViewHolder extends RecyclerView.ViewHolder {
        private final TextView tv_empty;

        public EmptyViewHolder(View itemView) {
            super(itemView);
            tv_empty = ((TextView) itemView.findViewById(R.id.tv_empty));
        }

        public void setEmptyText(String text) {
            if (tv_empty!=null) {
                tv_empty.setText(text);
            }
        }
    }
}
